<?php
import("@.ORG.Common");
import("@.ORG.Base");
class InfoAction extends Base
{
    public function index() {
        $model = M('daolu_info');
        $daoluData = $model->select();
        //$(":text").each(function(){ $(this).val($(this).attr("name")); });

        $infoModel = M('guanggao_info');
        $infoData = $infoModel->field('id,danwei_name,fuzheren_name,fuzheren_tel,lianxiren_name,lianxiren_tel,company_name,company_fuzheren_name,company_fuzheren_tel,company_lianxiren_name,company_lianxiren_tel,address,qingdan_lianxiren_name,qingdan_lianxiren_tel')->group(' danwei_name,company_name ')
            ->order('id DESC')->select();

        $this->assign('infoData',$infoData);
        $this->assign('daoluData',$daoluData);

        $this->assign('date',date('Y-m-d'));
        Layout("layout");
        $this->display();
    }

    public function add(){
        //dump($_POST);
        if( !empty($_POST) ){
            $row = $_POST;
//            $row['abc_yingyuezhizhao_checkbox'] == 1 ? '' : $row['abc_yingyuezhizhao'] = '';
//            $row['a_faren_shenfenzheng_checkbox'] == 1 ? '' : $row['a_faren_shenfenzheng'] = '';
//            $row['abc_shenqingzhe_shenfenzheng_checkbox'] == 1 ? '' : $row['abc_shenqingzhe_shenfenzheng'] = '';
//            $row['a_weituoshu_checkbox'] == 1 ? '' : $row['a_weituoshu'] = '';
//            $row['a_guihuaju_shenqingbiao_checkbox'] == 1 ? '' : $row['a_guihuaju_shenqingbiao'] = '';
//            $row['a_didian_chanquan_checkbox'] == 1 ? '' : $row['a_didian_chanquan'] = '';
//            $row['abc_zhizuo_gongshi_zhizhao_checkbox'] == 1 ? '' : $row['abc_zhizuo_gongshi_zhizhao'] = '';
//            $row['a_images_checkbox'] == 1 ? '' : $row['a_images'] = '';
//            $row['a_didian_celiangtu_checkbox'] == 1 ? '' : $row['a_didian_celiangtu'] = '';
//            $row['b_xuanchuanguazhang_shenqingbiao_checkbox'] == 1 ? '' : $row['b_xuanchuanguazhang_shenqingbiao'] = '';
//            $row['c_shigongzuoye_shenqingbiao_checkbox'] == 1 ? '' : $row['c_shigongzuoye_shenqingbiao'] = '';
//            $row['bc_xiaoguotu_checkbox'] == 1 ? '' : $row['bc_xiaoguotu'] = '';
//            $row['bc_qita_checkbox'] == 1 ? '' : $row['bc_qita'] = '';
//            $row['a_jiegouanquantuzhi_checkbox'] == 1 ? '' : $row['a_jiegouanquantuzhi_checkbox'] = '';
//            $row['a_zulinhetong_checkbox'] == 1 ? '' : $row['a_zulinhetong'] = '';


            $bianhaoArray = $this->createBianhao();
            if($row['type'] == 'a'){
                $row['a_bianhao'] = $bianhaoArray['a_bianhao'];
                $row['a_bianhao_num'] = $bianhaoArray['a_bianhao_num'];
            }
            $model = M('guanggao_info');
            $lastID = $model->add($row);
            if( $lastID ){
                if($row['type'] == 'a'){

                    $this->redirect('/Info/a/id/'.$lastID);
                }
                else{

                    $this->redirect('/Info/a/id/'.$lastID);
                }
            }
        }
        $this->redirect('/Info/index');
    }

    public function addAjax(){
        //dump($_POST);
        if( !empty($_POST) ){
            $row = $_POST;
            $bianhaoArray = $this->createBianhao();
            if($row['type'] == 'a'){
                $row['a_bianhao'] = $bianhaoArray['a_bianhao'];
                $row['a_bianhao_num'] = $bianhaoArray['a_bianhao_num'];
            }
            $model = M('guanggao_info');
            $lastID = $model->add($row);
            if( $lastID ){
                $this->returnAjax(0,'操作成功',array('id'=>$lastID));
            }
            $this->returnAjax(1002,'发生错误，操作失败',array('info'=>$model->getDbError()));
        }
        $this->returnAjax(1001,'发生错误，操作失败');
    }

    public function upimg(){
        $domain="public";
        if(!IS_SAE){//兼容普通环境
            $domain="Public/indexad";
        }
        $fileName = $_FILES["files"]["name"][0];
        $fileType = $_FILES["files"]["type"][0];
        $fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
        if( empty($fileName) ){
            $this->returnAjax(-1,'params error');
        }
        $newFileName = time().rand(1000,9999).'.'.$fileExt;
        $s = new SaeStorage();
        $ret = $s->upload( $domain,$newFileName,$_FILES["files"]["tmp_name"][0]);
        if( $ret ){
            $url = $ret;
            if(!IS_SAE){//兼容普通环境
                //$url = 'http://'.$_SERVER['HTTP_HOST'].'/'.$domain.'/'.$newFileName;
                $url = '/'.$domain.'/'.$newFileName;
            }
            $this->returnAjax(0,'',array('url'=>$url));
        }
        else{
            $this->returnAjax(-2,'upload error');
        }
    }

    public function a(){
        $model = M('guanggao_info');
        $id = (int)$_REQUEST["id"];
        if($id > 0){
            $info = $model->where(array("id"=>$id))->find();
            $typeText = $info['type'] == 'a' ? '广告招牌' : ($info['type'] == 'b' ? '临时广告' : '临时占道');
        }
        $this->assign("typeText",$typeText);
        $this->assign("info",$info);
        layout('qingdan');
        $this->display();
    }

    public function bc(){
        $model = M('guanggao_info');
        $id = (int)$_REQUEST["id"];
        if($id > 0){
            $info = $model->where(array("id"=>$id))->find();
        }
        $this->assign("info",$info);
        layout('qingdan');
        $this->display();
    }

    public function dengjibiao(){
        $model = M('guanggao_info');
        $id = (int)$_REQUEST["id"];
        if($id > 0){
            $info = $model->where(array("id"=>$id))->find();
        }
        $info['yijian'] = ereg_replace("\n", "<BR>\n", $info['yijian']);
        $info['remark'] = ereg_replace("\n", "<BR>\n", $info['remark']);
        $this->assign("info",$info);
        layout('qingdan');
        $this->display();
    }


    private function createBianhao(){
        //二、户外申请编号按日期改为：如 2017年7月3日的是20170703-001、20170703-002
        $model = M('guanggao_info');
        $data = $model->where(array('type'=>'a','receiving_time'=>date('Y-m-d')))->field('a_bianhao_num')->order('id DESC')->find();
        if($data){
            $a_bianhao_num = (int)$data['a_bianhao_num'] + 1;
        }
        else{
            $a_bianhao_num = '001';
        }

        $bianhao = '' . date("Ymd-") . str_pad($a_bianhao_num,3,0,STR_PAD_LEFT);
        return array('a_bianhao'=>$bianhao,'a_bianhao_num'=>$a_bianhao_num);
    }


}
